package com.shenmazong.demoes00.task;

import com.shenmazong.demoes00.mapper.TbProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
@Slf4j
public class SnapTask {

    @Autowired
    private TbProductMapper tbProductMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    private boolean snapJdFlag = false;
    private boolean insertEsFlag = false;

    @Scheduled(fixedDelay = 10000)
    public void snapJd() {
        if(snapJdFlag) {
            System.out.println("snapJd.....");
            SnapJdThread snapJdThread = new SnapJdThread();

            snapJdThread.setMapper(tbProductMapper);
            snapJdThread.start();

            snapJdFlag = false;
        }
    }

    @Scheduled(fixedDelay = 10000)
    public void insertEs() {
        if(insertEsFlag) {
            System.out.println("insertEs.....");
            DbToEsThread esThread = new DbToEsThread();

            esThread.setEsAndDb(elasticsearchTemplate, tbProductMapper);
            esThread.start();

            insertEsFlag = false;
        }
    }
}
